function y = hgmm_classify(X, hgmm, cluster_weight)

num_classes = size(hgmm.class_model, 1);
num_clusters = size(hgmm.cluster_model, 1);
num_points = size(X, 1);
y = zeros(num_points, 1);


cluster_logp = zeros(num_points, num_clusters);
for j = 1 : num_clusters
    fprintf('Calculating probabilities for cluster %d\n', hgmm.cluster_label(j));
    cluster_logp(:,j) = gmm_logpdf(X, hgmm.cluster_model{j}) + log(hgmm.cluster_prior(j));
end
cluster_logp = cluster_logp * cluster_weight;

y = y + double(hgmm.class_label(1));
best_logp = gmm_logpdf(X, hgmm.class_model{1}) + log(hgmm.class_prior(1)) + cluster_logp(:, hgmm.class_cluster(1));

for j = 2 : num_classes
    fprintf('Calculating probabilities for class %d\n', hgmm.class_label(j));
    cur_logp = gmm_logpdf(X, hgmm.class_model{j}) + log(hgmm.class_prior(j));
    cur_logp = cur_logp + cluster_logp(:, hgmm.class_cluster(j));
    for i = 1 : num_points
        if (cur_logp(i) > best_logp(i))
            y(i) = hgmm.class_label(j);
            best_logp(i) = cur_logp(i);
        end
    end
end
